<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/26
 * Time: 18:18
 */
namespace App\Http\Controllers\web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntBookInfo;
use App\Module\EntBookType;
use App\Module\EntBookSumUp;
use App\Module\EntMemberInfo;
use App\Module\EntBookImg;
use App\Module\EntBookCollect;
use App\Module\EntBookViewLog;
use DB;
use Validator;
use Redirect;
use Session;
use App\Models\V1\MemberModel;

class BookController extends Controller
{
    public function book()
    {
        //获取会员标识
        $memNumber = $this->return_MemNumber();
        //获取会员注册时间
        $entMemberInfo = EntMemberInfo::select('AddDate')->where('MemNumber', $memNumber)->where('States', 1)->first();
        $memTime = $entMemberInfo->AddDate;
        $entBookInfohot = DB::table('VW_WEB_BookInfo')->select('BookID', 'IsGratis', 'BookTitle', 'BookDesc', 'FilePath1', 'IsShow', 'DeleteMark', 'ReleaseDate', 'AddDate')->where('IsShow', 1)->where('DeleteMark', 1)->where('SortNumber', 1)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'desc')->first();
        //判断置顶书籍 0-没有 1-有
        $hot = 0;
        if (count($entBookInfohot) > 0) {
            $hot = $entBookInfohot->BookID;
            //程序调整书籍排列顺序0不显示 1显示
            if ($entBookInfohot->IsGratis == 0) {
                $hotzero1 = $entBookInfohot->AddDate;
                $hotzero2 = $memTime;
                if (strtotime($hotzero1) > strtotime($hotzero2)) {
                    $entBookInfohot->IsGratis = 1;
                }
            }
        }
        $entBookInfo = DB::table('VW_WEB_BookInfo')->select('BookID', 'IsGratis', 'BookTitle', 'BookDesc', 'FilePath1', 'IsShow', 'DeleteMark', 'ReleaseDate', 'AddDate')->where('BookID', '<>', $hot)->where('IsShow', 1)->where('DeleteMark', 1)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'desc')->get();

        if (count($entBookInfo) > 0) {
            //程序调整书籍排列顺序0不显示 1显示
            foreach ($entBookInfo as $data) {
                if ($data->IsGratis == 0) {
                    $zero1 = $data->AddDate;
                    $zero2 = $memTime;
                    if (strtotime($zero1) > strtotime($zero2)) {
                        $data->IsGratis = 1;
                    }
                }
            }
        }
        //取出该会员所有收藏的书籍
        $collectList = EntBookCollect::select('BookID')->where('MemNumber', $memNumber)->get();
        $colllist = '';
        if (count($collectList) > 0) {
            foreach ($collectList as $data) {
                $colllist .= $data->BookID . '_';
            }
        }
        //取得书籍分类
        $entBookType = EntBookType::select('TypeID', 'TypeName')->where('DeleteMark', 1)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'desc')->get();
        $page = 0;
        $MPage = Session::get('MPage');
        if ($MPage != '') {
            $page = 1;
        } else {
            Session::put('MPage', '3');
        }
        return view('web/book/book', ['entBookInfohot' => $entBookInfohot, 'entBookInfo' => $entBookInfo, 'collectList' => $colllist, 'entBookType' => $entBookType,'page'=>$page]);
    }


    public function bookdetail($id)
    {
        try {
            //获取会员标识
            $memNumber = $this->return_MemNumber();
            //获取书籍详细
            $entBookInfo = EntBookInfo::findOrFail($id);
            //获取改进计划
            $entBookSumUp = EntBookSumUp::select('SumUpID', 'BookReview1', 'BookReview2', 'BookReview3','BookReview4')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
            //获取PPT
            $entBookImg = EntBookImg::select('FilePath1', 'SortNumber')->where('BookID', $id)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'asc')->get();
            //验证是否收藏过该书籍 0-未收藏 1-已收藏
            $coll = 0;
            $entBookCollectCount = EntBookCollect::select('ColID')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
            if (count($entBookCollectCount) > 0) {
                $coll = 1;
            }
        } catch (ModelNotFoundException $e) {
            throw new NotFoundHttpException();
        }
        return view('web/book/bookdetail', ['entBookInfo' => $entBookInfo, 'entBookSumUp' => $entBookSumUp, 'entBookImg' => $entBookImg, 'coll' => $coll]);
    }

    public function audiodetail($id)
    {
        try {
            //获取会员标识
            $memNumber = $this->return_MemNumber();
            //获取书籍详细
            $entBookInfo = EntBookInfo::findOrFail($id);
            //获取改进计划
            $entBookSumUp = EntBookSumUp::select('SumUpID', 'BookReview1', 'BookReview2', 'BookReview3','BookReview4')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
            //获取PPT
            $entBookImg = EntBookImg::select('FilePath1', 'SortNumber')->where('BookID', $id)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'asc')->get();
            //验证是否收藏过该书籍 0-未收藏 1-已收藏
            $coll = 0;
            $entBookCollectCount = EntBookCollect::select('ColID')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
            if (count($entBookCollectCount) > 0) {
                $coll = 1;
            }
        } catch (ModelNotFoundException $e) {
            throw new NotFoundHttpException();
        }
        return view('web/book/audiodetail', ['entBookInfo' => $entBookInfo, 'entBookSumUp' => $entBookSumUp, 'entBookImg' => $entBookImg, 'coll' => $coll]);
    }

    /*
     * 添加读书改进计划
     */
    public function reviewstore(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $id = $request->json('id');
                $review1 = $request->json('review1');
                $review2 = $request->json('review2');
                $review3 = $request->json('review3');
                $review4 = $request->json('review4');
                $memNumber = $this->return_MemNumber();
                $entBookSumUpCount = EntBookSumUp::select('SumUpID')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
                if (count($entBookSumUpCount) > 0) {
                    $entBookSumUp = EntBookSumUp::find($entBookSumUpCount->SumUpID);
                    $entBookSumUp->BookReview1 = $review1;
                    $entBookSumUp->BookReview2 = $review2;
                    $entBookSumUp->BookReview3 = $review3;
                    $entBookSumUp->BookReview4 = $review4;
                    $entBookSumUp->save();
                    if ($entBookSumUp) {
                        $result = 'ok';
                    } else {
                        $result = 'no';
                    }
                } else {
                    $entBookSumUp = new EntBookSumUp();
                    $entBookSumUp->BookID = $id;
                    $entBookSumUp->MemNumber = $memNumber;
                    $entBookSumUp->BookReview1 = htmlentities($review1);
                    $entBookSumUp->BookReview2 = htmlentities($review2);
                    $entBookSumUp->BookReview3 = htmlentities($review3);
                    $entBookSumUp->BookReview4 = htmlentities($review4);
                    $entBookSumUp->AddDate = date('Y-m-d H:i:s', time());
                    $entBookSumUp->save();
                    if ($entBookSumUp) {
                        $result = 'ok';
                    } else {
                        $result = 'no';
                    }
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }

    /*
     * 添加收藏记录
     */
    public function collectstore(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $id = $request->json('id');
                $entBookInfo = EntBookInfo::findOrFail($id);
                $memNumber = $this->return_MemNumber();
                $entBookCollectCount = EntBookCollect::select('ColID')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
                if (count($entBookCollectCount) > 0) {
                    EntBookCollect::destroy($entBookCollectCount->ColID);
                    $result = '1';
                } else {
                    $entBookCollect = new EntBookCollect();
                    $entBookCollect->BookID = $id;
                    $entBookCollect->MemNumber = $memNumber;
                    $entBookCollect->FilePath1 = $entBookInfo->FilePath1;
                    $entBookCollect->AddDate = date('Y-m-d H:i:s', time());
                    $entBookCollect->save();
                    if ($entBookCollect) {
                        $result = '2';
                    } else {
                        $result = '3';
                    }
                }
            }
        } catch (Exception $e) {
            $result = '3';
        }
        return $result;
    }

    protected function return_MemNumber()
    {
        $i = 0;
        try {
            $MName = Session::get('MName');
            $MKey = Session::get('MKey');
            if ($MName != '' && $MKey != '') {
                $entMemberInfo = EntMemberInfo::select('MemNumber')->where('UserName', $MName)->where('LoginKey', $MKey)
                    ->where('DeleteMark', '1')
                    ->where('States', '1')
                    ->first();
                if (count($entMemberInfo) > 0) {
                    $i = $entMemberInfo->MemNumber;
                }
            }
        } catch (ModelNotFoundException $e) {
            $i = 0;
        }
        return $i;
    }

    //查询
    public function searchType($keyword = '')
    {

        return view('search');
    }

    public function searchKeyWord(Request $request)
    {
        //获取会员标识
        $memNumber = $this->return_MemNumber();
        //获取会员注册时间
        $entMemberInfo = EntMemberInfo::select('AddDate')->where('MemNumber', $memNumber)->where('States', 1)->first();
        $memTime = $entMemberInfo->AddDate;
        if ($request->ajax()) {
            $keyword = $request->json('keyword');
            if ($keyword != '') {
                $entBookInfo = EntBookInfo::select('BookID', 'BookTitle','IsGratis')->where('IsShow', 1)->where('DeleteMark', 1)
                    ->where(function ($query) use ($keyword) {
                        $query->orwhere('BookTitle', 'like', '%' . $keyword . '%')
                            ->orwhere('BookDesc', 'like', '%' . $keyword . '%');
                    })
                    ->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'desc')->get();
                if (count($entBookInfo) > 0) {

                    //程序调整书籍排列顺序0不显示 1显示
                    foreach ($entBookInfo as $data) {
                        if ($data->IsGratis == 0) {
                            $zero1 = $data->AddDate;
                            $zero2 = $memTime;
                            if (strtotime($zero1) > strtotime($zero2)) {
                                $data->IsGratis = 1;
                            }
                        }
                    }
                }
                return $entBookInfo;
            }
        } else {
            $entBookInfo = EntBookInfo::select('BookID', 'BookTitle','IsGratis')->where('IsShow', 1)->where('DeleteMark', 1)->orderBy('SortNumber', 'asc')->orderBy('AddDate', 'desc')->take(6)->get();
            if (count($entBookInfo) > 0) {

                //程序调整书籍排列顺序0不显示 1显示
                foreach ($entBookInfo as $data) {
                    if ($data->IsGratis == 0) {
                        $zero1 = $data->AddDate;
                        $zero2 = $memTime;
                        if (strtotime($zero1) > strtotime($zero2)) {
                            $data->IsGratis = 1;
                        }
                    }
                }
            }
            return view('web/book/search', ['entBookInfo' => $entBookInfo]);
        }
    }

    public function videoplayLog(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $id = $request->json('id');
                $entBookInfo = EntBookInfo::findOrFail($id);
                $memNumber = $this->return_MemNumber();
                $entBookViewLogCount = EntBookViewLog::select('ViewLogID')->where('MemNumber', $memNumber)->where('BookID', $id)->first();
                if (count($entBookViewLogCount) == 0) {
                    $entBookViewLog = new EntBookViewLog();
                    $entBookViewLog->BookID = $id;
                    $entBookViewLog->MemNumber = $memNumber;
                    $entBookViewLog->FilePath1 = $entBookInfo->FilePath1;
                    $entBookViewLog->StartTime = date('Y-m-d H:i:s', time());
                    $entBookViewLog->EndTime = '';
                    $entBookViewLog->AddDate = date('Y-m-d H:i:s', time());
                    $entBookViewLog->save();
                    if ($entBookViewLog) {
                        $result = '2';
                    } else {
                        $result = '3';
                    }
                }
            }
        } catch (Exception $e) {
            $result = '3';
        }
        return $result;
    }


    public function login(){
        return view('web/book/login');
    }

    public function login_action(Request $request){
        $mobile = $request->input('txtMobile');
        $memberModel    = new MemberModel();
        $memberInfo     = $memberModel->get_member_info_by_mobile($mobile);
        if($memberInfo->States == 0){
            return "<h1>权限不足</h1>";
        }
        $loginKey = md5(date('YmdHis', time()));
        Session::put('MName', $memberInfo->UserName);
        Session::put('MKey', $loginKey);
        $memberModel->update_member_info(['UserID'=>$memberInfo->UserID,'LoginKey'=>$loginKey]);
        return redirect('book.html');
    }


}